let { generateKeyPairSync, privateEncrypt, publicDecrypt } = require('crypto');
//生成一对密钥对，一个是公钥，一个私钥
let rsa = generateKeyPairSync('rsa', {
    modulusLength: 1024,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'//base64格式的私钥
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: 'passphrase'
    }
});
let message = 'hello';
let encryptedMessage = privateEncrypt({
    key: rsa.privateKey, passphrase: 'passphrase'
}, Buffer.from(message, 'utf8'));
console.log('encryptedMessage', encryptedMessage);
let decryptedMessage = publicDecrypt(rsa.publicKey, encryptedMessage);
console.log('decryptedMessage', decryptedMessage.toString());


